//
//  BarQrcodeViewController.m
//  SinaWeibo
//
//  Created by Apple on 16/8/8.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "BarQrcodeViewController.h"
#import "AVFoundation/AVFoundation.h"


@interface BarQrcodeViewController ()<AVCaptureMetadataOutputObjectsDelegate>


- (IBAction)cilckClose:(id)sender;



@property (weak, nonatomic) IBOutlet UIView *barQrcodeView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *barQrcodeConsY;

@property (strong, nonatomic) AVCaptureSession *session;

@property (weak, nonatomic) AVCaptureVideoPreviewLayer *previewLayer;

@property (weak, nonatomic) CADisplayLink *displayLink;

@end

@implementation BarQrcodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //将扫面添加到循环中
    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(slide)];
    
    [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    
    //添加输入设备
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    //添加输入设备对象
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:NULL];
    
    //添加输出设备对象并设置代理
    AVCaptureMetadataOutput *metaDataOutput = [[AVCaptureMetadataOutput alloc]init];
    
    [metaDataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    //将输入输出添加到会话中
    AVCaptureSession *session = [[AVCaptureSession alloc]init];
    self.session = session;
    if ([session canAddInput:deviceInput]) {
        [session addInput:deviceInput];
    }
    if ([session canAddOutput:metaDataOutput]) {
        [session addOutput:metaDataOutput];
    }
    
#warning 一定要添加完之后再设置输出类型
    //设置输出类型
    [metaDataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeUPCECode]];
    
    //设置预览界面
    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
    previewLayer.frame = self.view.bounds;

    self.previewLayer = previewLayer;
    
    [self.view.layer insertSublayer:previewLayer atIndex:1];
    
    //会话开始采集数据
    [session startRunning];
    
}

-(void)slide{
    
    self.barQrcodeConsY.constant -=5;
    if (self.barQrcodeConsY.constant == -150) {
        self.barQrcodeConsY.constant = 150 ;
    }
    
}

#pragma mark ----- AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    
    //如解析道数据
    if (metadataObjects > 0) {
        //停止扫描
        [self.session stopRunning];
        
        //停止运行循环
        [self.displayLink invalidate];
        self.displayLink = nil;
        
        //移除预览界面
        [self.previewLayer removeFromSuperlayer];
        
        //移除扫描框
        [self.barQrcodeView removeFromSuperview];
        
        //取出数据
        AVMetadataMachineReadableCodeObject *codeObj = metadataObjects.lastObject;
        
        //显示数据到界面上
        UILabel *label = [[UILabel alloc]initWithFrame:self.view.bounds];
        
        label.text = codeObj.stringValue;
        
        [self.view addSubview:label];
        
    }
    
    
}




- (IBAction)cilckClose:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"关闭");
    }];
}
@end
